import { fetchHome } from '../../services/home/home';
import Toast from 'tdesign-miniprogram/toast/index';
import i18nInstance from "../../miniprogram_npm/miniprogram-i18n-plus/index"
import ActionSheet, { ActionSheetTheme } from 'tdesign-miniprogram/action-sheet/index';
import { config } from "../../config/index"

Page({
  data: {
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    peiliaolist: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 0,
    current_swiper: 0,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: { type: 'dots' },
    swiperImageProps: { mode: 'scaleToFill' },
  },
  goodListPagination: {
    index: 0,
    num: 20,
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
    i18nInstance.effect(this);
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();

  },

  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({ swiper, tabList }) => {
      this.setData({
        tabList,
        imgSrcs: swiper,
        pageLoading: false,
      });
      this.loadGoodsList(true);
    });
  },

  onReTry() {
    this.loadGoodsList();
  },

  handleAction() {
    ActionSheet.show({
      theme: ActionSheetTheme.List,
      selector: '#t-action-sheet',
      context: this,
      items: [
        {
          label: '10086',
        },
        {
          label: '10000',
        }
      ],
    });
  },
  handleSelected(e) {
    console.log(e.detail);
    switch (e.detail.index) {
      case 0:
        wx.makePhoneCall({
          phoneNumber: '10086'
        });
        break;
      case 1:
        wx.makePhoneCall({
          phoneNumber: '10000'
        })
        break;
      default:
        // 处理其他情况
        console.log("未知选项");
        break;
    }
  },
  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({ goodsListLoadStatus: 1 });
    try {
      const nextList = [
        {
          id: 1,
          title: "H9S单滴系列回转式制瓶机",
          subtitles: "360°同步正吹气机构,旗舰产品",
          src: "https://krseoul.imgtbl.com/i/2024/08/05/66b02279af57c.jpg",
          num: 1
        },
        {
          id: 2,
          title: "H9双滴系列回转式制瓶机",
          subtitles: "成模360冷却,旗舰产品",
          src: "https://krseoul.imgtbl.com/i/2024/08/05/66b02279af57c.jpg",
          num: 1
        },
        {
          id: 3,
          title: "H8S系列回转式制瓶机",
          subtitles: "成模360冷却,旗舰产品",
          src: "https://krseoul.imgtbl.com/i/2024/08/05/66b02279af57c.jpg",
          num: 1
        },
        {
          id: 4,
          title: "多料重伺服供料机",
          subtitles: "与制瓶机实现同步,制瓶机最佳伴侣",
          src: "https://krseoul.imgtbl.com/i/2024/08/05/66b02279af57c.jpg",
          num: 1
        }
      ]
      const nextListSec = [
        {
          id: 1,
          title: "CDPL-1型",
          subtitle: "适合为出料量30~80吨/天的单个或多个窑炉供料",
          src: "https://krseoul.imgtbl.com/i/2024/08/05/66b040103da02.jpg",
          num: 1,
          Desc: "CDPL-1 suitable for feeding of single/multiple furnace with a capacity of 30-80 tons/day"
        },
        {
          id: 2,
          title: "CDPL-2型",
          subtitle: "适合为出料量60~200吨/天的单个或多个窑炉供料",
          src: "https://krseoul.imgtbl.com/i/2024/08/05/66b040103da02.jpg",
          num: 1,
          Desc: "CDPL-1 suitable for feeding of single/multiple furnace with a capacity of 30-80 tons/day"
        }
      ]
      // var that = this;
      // wx.request({
      //   url: config.baseUrl + "/GetmachineList?lang=" + wx.getStorageSync('lang'),
      //   method: "GET",
      //   success(e) {
      //     console.log(e.data);
      //     that.setData({
      //       goodsList: e.data
      //     })
      //   }
      // })
      this.setData({
        goodsList: nextList,
        peiliaolist: fresh ? nextListSec : this.data.peiliaolist.concat(nextListSec),
      });
      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({ goodsListLoadStatus: 3 });
    }
  },

  handleCardClickjqr: function (event) {
    wx.navigateTo({
      url: `/pages/goods/jqr-detail/index`,
    });
  },
  handleCardClickjqr2: function (event) {
    wx.navigateTo({
      url: `/pages/goods/jqr-detail/index`,
    });
  },


  navToSearchPage() {
    wx.navigateTo({ url: '/pages/goods/search/index' });
  },

  tabChangeHandle: function (event) {
    this.setData({
      current: event.detail.value  // 更新current值
    });
  }
});
